import java.util.Arrays;
import java.util.Comparator;

/**
 * Created by xiebang
 * Description
 * User:小谢
 * Date:2021-10-20
 * Time:17:12
 */
public class TestDemo {
    class Solution {
        public int carFleet(int target, int[] position, int[] speed) {
            int n = speed.length;
            if( n<=1 ){
                return n;
            }
            double[][] cars = new double[n][2];
            // 将速度换算成时间，更加直观，并且将速度和终点两个因素合并了；
            for (int i = 0; i < n; i++) {
                cars[i][0] = position[i];
                cars[i][1] = (double) (target-position[i])/speed[i];
            }
            //升序排列
            Arrays.sort(cars, Comparator.comparingDouble(a->a[0]) );


            double maxTime =cars[n-1][1];
            int ans = 0;
            for (int i = n-1; i >=1; i--) {
                //起点大的，时间又小（更快到终点），则单独作为一个车队  ans++
                //起点大的，时间长，会被起点小的追上，则与下一车作为同一车队
                if( maxTime <  cars[i-1][1] ){
                    ans++;
                    maxTime = cars[i-1][1];
                }

            }
            //最后一个
            ans++;


            return ans;
        }
    }
}
